package com.lzq.ribbon.Controller;

import com.lzq.ribbon.Entity.Customer;
import com.lzq.ribbon.client.searchClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
public class customerController {
   @Autowired
    private RestTemplate restTemplate;
   /* @Autowired
    private EurekaClient eurekaClient;*/
    @Autowired
    private searchClient searchClient;

    @GetMapping("/customer")
    @HystrixCommand(fallbackMethod = "customerFallBack")
    public String customer(){
        //测试hytirx使用的是哪一些线程。
        System.out.println("+++++++++"+Thread.currentThread().getName());
        //int i = 1/0;
        //Ribbon方式
        //return this.restTemplate.getForObject("http://PRODUCER-SERVICE/search",String.class);

        //Feign方式
        String res = searchClient.search();
        return res;
    }
    public String customerFallBack(){
        System.out.println("=============");
        return "您的客户请求出错了！！！";
    }

    @GetMapping("/search/{id}")
    @HystrixCommand(fallbackMethod = "findByidFallBack")
    public Customer findByid(@PathVariable Integer id){
        //测试hytirx使用的是哪一些线程。
        System.out.println(Thread.currentThread().getName());
        int i = 1/0;
        return searchClient.findByid(id);
    }
    //findByid的降级方法，方法的描述要和接口一致
    public Customer findByidFallBack(@PathVariable Integer id){
        System.out.println("=============");
        System.out.println(new Customer(-1,"luzequan",0));
        return new Customer(-1,"对不起出错了",0);
    }

    @GetMapping("/getCustomer")
    public Customer getCustomer(@RequestParam Integer id, @RequestParam String name){
       return searchClient.getCustomer(id,name);
    }

    @GetMapping("/save")//会自动转为POST请求，出现405错误
    public Customer save(Customer customer){
        return searchClient.save(customer);
    }

    @GetMapping("/testZuul")
    public String testZuul(){
        String res = searchClient.search();

        int i = 1/0;
        return res;
    }


}
